package nl.hanze.designpatterns.mvc.controller;

import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import nl.hanze.designpatterns.domain.TroubleTicket;
import nl.hanze.designpatterns.mvc.model.TroubleTicketModel;
import nl.hanze.designpatterns.mvc.view.TroubleTicketView;

public class TroubleTicketController extends Controller<TroubleTicketModel> {

	public TroubleTicketController(TroubleTicketModel model) {
		super(model);
	}
	
	public void setSelectedTicket(int row){
		this.getModel().getSelectTicket(row);
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {
		
		switch (e.getActionCommand()) {
		case "trouble_ticket_add":
			
			JButton button = (JButton) e.getSource();
			String caller = ((JTextField)button.getClientProperty("caller")).getText();
			String description = ((JTextArea)button.getClientProperty("description")).getText();
			
			getModel().addTroubleTicket(caller,description);
			getModel().notifyViews();
			break;
		case "show_tickets_caller":
			JComboBox<String> callerName = (JComboBox<String>)e.getSource();
			TroubleTicketView view = (TroubleTicketView)getModel().getView("troubeTicketOverview");
			view.showTicketsOfCaller(getModel().convertTroubleTicketsToArray(getModel().getTroubleTicketByName((String)callerName.getSelectedItem())));
			break;
		case "reshow_all_tickets":
			TroubleTicketView all = (TroubleTicketView)getModel().getView("troubeTicketOverview");
			all.showAllTickets();
			break;

		default:
			break;
		}
		
		super.actionPerformed(e);
	}

}
